using UnityEngine;
using System.Collections;

public class FailAndMiss : MonoBehaviour {

	public GameObject Platform1;
	public GameObject Platform2;
	public GameObject Platform3;
	public GameObject bomb;
	public GameObject FailBottom;
	public bool over = false;
	string names = " ";
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		/*if(Platform1 == false && Platform2 == false && Platform3 == false)
		{
			Application.LoadLevel(4);// for game over screen
		}*/
	}
	
	void OnGUI()
	{
		if(over)
		{
		GUI.Label(new Rect((Screen.width/2)-25,Screen.height/2-25,100,25),"Game Over");
		names = GUI.TextField(new Rect((Screen.width/2)-25,Screen.height/2,100,25),names);
	
			if(GUI.Button(new Rect ((Screen.width/2)-25,Screen.height/2+25,100,25),"Submit"))
			{
			addScore(names,ScoreKeeper.scores);
			ScoreKeeper.scores = 0;
			Application.LoadLevel(0);	
			}
		}
		
	}
	
	void OnCollisionEnter(Collision collision)
	{//upon collisio 
			
		if( Platform1 == true)
		{
			Object.Destroy(Platform1);
			ScoreKeeper.scores-=10;
		}
		if(Platform1 == false && Platform2 == true)
		{
			Object.Destroy(Platform2);
			ScoreKeeper.scores-=10;
			
		}
		if(Platform1 == false && Platform2 == false && Platform3 == true)
		{
			Object.Destroy(Platform3);
			ScoreKeeper.scores-=10;
			Time.timeScale = 0;
			over = true;
			
			
		}
        audio.Play();		
	}

	
	public void addScore(string name, int scorer)
	{
		int newScore, oldScore;
		string newName,oldName;
		
		newName = name;
		newScore = scorer;
		
		for(int i=0;i<3;i++){
		if(PlayerPrefs.HasKey(i+"HScore")){
				if(PlayerPrefs.GetInt(i+"HScore")<newScore){ 
            // new score is higher than the stored score
            oldScore = PlayerPrefs.GetInt(i+"HScore");
            oldName = PlayerPrefs.GetString(i+"HScoreName");
            PlayerPrefs.SetInt(i+"HScore",newScore);
            PlayerPrefs.SetString(i+"HScoreName",newName);
            newScore = oldScore;
            newName = oldName;

		         }
      }else{
         PlayerPrefs.SetInt(i+"HScore",newScore);
         PlayerPrefs.SetString(i+"HScoreName",newName);
         newScore = 0;
         newName = "";
      }
   }

		
	}
}
